﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.database;
using System.Data;
using System.Data.OleDb;
using ERP_RORZE_PERMISSION.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;

namespace ERP_RORZE_PRODUCT.Product.UserControls
{
    public partial class ctrlBusOderQtyChange : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler Reload;
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        DataTable dt;
        ClsBusOderTable b = new ClsBusOderTable();
        ClsMOTable MO = new ClsMOTable();
        ClsMOOptionTable MOO = new ClsMOOptionTable();
        private LogUtils log;
        

        public ctrlBusOderQtyChange()
        {
            InitializeComponent();
            cbBusOderNo.LostFocus += cbBusOderNo_LostFocus;
            log = new LogUtils();
        }

        void cbBusOderNo_LostFocus(object sender, EventArgs e)
        {
            if (!cbBusOderNo.Text.Equals(string.Empty))
            {
                string sql = "select [242_BusOder].*, " +
                         " case when MONo IS NULL then '0' else MONo end AS MONo1, " +
                         " case when MOQty IS NULL then 0 else MOQty end AS MOQty1, " +
                         " case when Changedate IS NULL then 0 else Changedate end AS ChangeDate1," +
                         " case when Change IS NULL then '' else Change end AS Change1" +
                         " from [242_BusOder] where BOderNo ='" + cbBusOderNo.Text + "'";
                dt = BaseDAO.ExecuteQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    txtMONo.Text = dt.Rows[0]["MONo1"].ToString();
                    txtMOQty.Text = dt.Rows[0]["MOQty1"].ToString();
                    txtOderQty.Text = dt.Rows[0]["Qty"].ToString();
                    txtPlanNo.Text = dt.Rows[0]["planNo"].ToString();
                    checkFinish.Checked = (bool)dt.Rows[0]["Finished"];

                }
                else
                {
                    MessageBox.Show("Hãy kiểm tra lại order.");
                    dateUpdateDate.Focus();
                }
            }
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (checkFinish.Checked == true)
            {
                MessageBox.Show("Đơn hàng đã kết thúc, không thể thay đổi.");
            }
            else if (txtNewQty.Text == "")
            {
                MessageBox.Show("Chưa nhập NewQty!");

            }
            else if (txtNewQty.Text != "")
            {
                try
                {
                    int.Parse(txtNewQty.Text);
                    if (txtMONo.Text != "0" && (int.Parse(txtNewQty.Text) > int.Parse(txtMOQty.Text)))
                    {
                        MessageBox.Show("Kiểm tra lại số lượng!");
                        txtNewQty.Focus();
                    }
                    else
                    {
                        // Update vào BusOder
                        b.Qty = int.Parse(txtNewQty.Text);
                        b.Change = "Quantity changed";
                        b.ChangeDate = dateUpdateDate.DateTime;
                        b.BOderNo = cbBusOderNo.Text;
                        clsBusOderDAO.QtyChangeBusOder(b);

                        //write Log
                        log.Table = LogConstant.Table.Product.BusOrderList;
                        log.Task = LogConstant.Task.Update;
                        log.Description = "Change Quantity BOrderNo: " + b.BOderNo;
                        log.WriteLog();

                        MessageBox.Show("Đã cập nhật thành công");
                        txtNewQty.Text = "";
                        cbBusOderNo.Focus();
                        Reload(sender, e);
                    }
                }
                catch
                {
                    MessageBox.Show("New Qty phải là kiểu số nguyên!");
                }
            }
        }

        private void ctrlBusOderQtyChange_Load(object sender, EventArgs e)
        {
            //dt = clsBusOderDAO.GetAll();
            //List<ClsBusOderTable> objs = new List<ClsBusOderTable>();
            //foreach (DataRow r in dt.Rows)
            //{
            //    ClsBusOderTable obj = new ClsBusOderTable();
            //    obj.BOderNo = r["BOderNo"].ToString();
            //    obj.PlanNo = r["planNo"].ToString();
            //    obj.MONo = r["MONo1"].ToString();
            //    obj.ChangeDate = (DateTime)r["ChangeDate1"];
            //    obj.Finished = (bool)r["Finished"];
            //    obj.MOQty = (int)r["MOQty1"];
            //    obj.Qty = (int)r["Qty"];
            //    objs.Add(obj);
            //}
            //cbBusOderNo.DataSource = objs;
            //cbBusOderNo.DisplayMember = "BOderNo";

            dateUpdateDate.EditValue = DateTime.Now;
        }

        private void cbBusOderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ClsBusOderTable obj = ((ClsBusOderTable)cbBusOderNo.SelectedItem);
            //txtMONo.Text = obj.MONo.ToString();
            //txtMOQty.Text = obj.MOQty.ToString();
            //txtOderQty.Text = obj.Qty.ToString();
            //txtPlanNo.Text = obj.PlanNo.ToString();
            //checkFinish.Checked = obj.Finished;

        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

















        //public OleDbConnection Connect()
        //{
        //    //FileDialog pathFile = new OpenFileDialog();
        //    //if (pathFile.Filter()) 
        //    //    _cn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathFile + ";" + "Extended Properties=Excel 12.0;";
        //    //else
        //    //    _cn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " + pathFile + ";" + "Extended Properties=Excel 8.0;";
        //    //_con = new OleDbConnection(_cn);
        //    //_con.Open();
        //    //return _con;
        //}

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void labelControl7_Click(object sender, EventArgs e)
        {

        }
    }
}
